/**
 * Project Name:culiulib File Name:BaseCoeFragment.java Package Name:com.culiu.core.fragment Date:2014-11-14下午12:02:53 Copyright (c)
 * 2014, adison All Rights Reserved.
 */

package com.kikikeji.weizhuo.activity.core;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kikikeji.weizhuo.util.ViewFinder;
import com.kikikeji.weizhuo.util.ViewUtils;


public abstract class BaseCoreFragment extends Fragment implements IBaseFragment {

    protected ViewFinder mViewFinder;

    protected Activity mActivity;

    protected Bundle mDataIn;

    protected boolean mIsFragmentDetached = true;
    
    private boolean mIsFragmentHidden = false;

    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    protected View rootView = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
        mIsFragmentDetached = false;
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(rootView == null) {
            rootView = createView(inflater, container, savedInstanceState);
            executeOnceAfterCreateView();
        }
        ViewGroup parent = (ViewGroup)rootView.getParent();
        if(parent != null) {
            parent.removeView(rootView);
        }
        mViewFinder = new ViewFinder(rootView);
        return rootView;
    }
    protected void executeOnceAfterCreateView() {

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(rootView != null && rootView.getParent() != null) {
            ((ViewGroup)rootView.getParent()).removeView(rootView);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        mIsFragmentDetached = true;
        super.onDetach();
        mActivity = null;
    }

    @Override
    public void onStop() {
        super.onStop();
        showStatus("onStop");
    }

    @Override
    public void onResume() {
        super.onResume();
        showStatus("onResume");
    }

    @Override
    public void onComeIn(Bundle data) {
        mDataIn = data;
        showStatus("onComeIn");
    }

    @Override
    public void onBack(Bundle data) {
        showStatus("onBack");
    }


    public void showStatus(String status) {
    }

    protected BaseCoreFragment show(final View view) {
        ViewUtils.setGone(view, false);
        return this;
    }

    protected BaseCoreFragment hide(final View view) {
        ViewUtils.setGone(view, true);
        return this;
    }

    public boolean isFragmentDetached() {

        return mIsFragmentDetached;
    }

    public boolean isDead() {
        if(isFragmentDetached() || getActivity() == null || getActivity().isFinishing()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mIsFragmentHidden = hidden;
    }

    public boolean isPageVisible(){
        if(isHidden() || ! getUserVisibleHint() || mIsFragmentHidden)
            return false;
        return true;
    }

}
